### Nonparametric relationship between Grades and Social Status: White, Brown, and Black ------

data <- read.csv("intermediary_outputs/base_with_connection_data.csv")

# Define function to compute nonparametric regressions:
create_plots_data_np <- function(x_vector, y_vector, crit_sel)  {

  sample_selected <- which(crit_sel)
  
  bw_opt <- npregbw(xdat=x_vector[sample_selected], 
                    ydat=y_vector[sample_selected]) 
  
  if (bw_opt$bw > .5) {
    original_bw <- bw_opt$bw
    bw_opt$bw <- .3
  } else {
    print("All fine")
    original_bw <- 0
  }
  
  x_vector_data  <- x_vector[sample_selected]
  y_vector_data    <- y_vector[sample_selected]
  
  numerator_result <- npksum(txdat=x_vector_data, tydat=y_vector_data, 
                             bws=bw_opt$bw)$ksum 
  
  denominator_result <- npksum(txdat=x_vector_data, 
                               bws=bw_opt$bw)$ksum
  
  fit.lc_result <- numerator_result/denominator_result
  
  numerator_result_2 <- npksum(txdat=x_vector_data, tydat=y_vector_data,
                               bws=2*bw_opt$bw)$ksum 
  denominator_result_2 <- npksum(txdat=x_vector_data,
                                 bws=2*bw_opt$bw)$ksum
  
  fit.lc_result_2 <- numerator_result_2/denominator_result_2
  
  numerator_result_3 <- npksum(txdat=x_vector_data, tydat=y_vector_data, 
                               bws=3*bw_opt$bw)$ksum 
  denominator_result_3 <- npksum(txdat=x_vector_data,
                                 bws=3*bw_opt$bw)$ksum
  
  fit.lc_result_3 <- numerator_result_3/denominator_result_3
  
  return_result <-list()
  return_result[[1]] <- as.data.frame(cbind(x_vector_data, fit.lc_result, 
                                            fit.lc_result_2, fit.lc_result_3))
  
  return_result[[2]] <- bw_opt$bw
  return_result[[3]] <- original_bw
  
  return(return_result)
}


## Same race friends:
# White students:  
criteria_selection_wh <- is.na(data$grades_score) == F & 
  !is.na(data$ssi_same_negro_na_2_std) == 1 & data$race_simple == 1 & 
  data$grades_score > -2 & data$grades_score < 2 &
  data$flag_select_sample == 0 & is.na(data$id_interview) == F

plots_wh <- create_plots_data_np(data$grades_score, 
                                 data$ssi_same_negro_na_2_std,
                                 criteria_selection_wh)

# Nonwhite students:  
criteria_selection_nw <- is.na(data$grades_score) == F & 
  !is.na(data$ssi_same_negro_na_2_std) == 1 &  
  (data$race_simple == 3 | data$race_simple == 2) & 
  data$grades_score > -2 & data$grades_score < 2 & 
  data$flag_select_sample == 0 & is.na(data$id_interview) == F

plots_nw <- create_plots_data_np(data$grades_score, 
                                 data$ssi_same_negro_na_2_std, 
                                 criteria_selection_nw)

## Other race friends:
criteria_selection_wh_other <- 
  is.na(data$grades_score) == F & 
  !is.na(data$ssi_other_race_negro_na_2_std) == 1 & 
  data$race_simple == 1 & 
  data$grades_score > -2 & data$grades_score < 2 &
  data$flag_select_sample == 0 & 
  is.na(data$id_interview) == F

plots_wh_other <- create_plots_data_np(data$grades_score, 
                                       data$ssi_other_race_negro_na_2_std,
                                       criteria_selection_wh)

criteria_selection_nw_other <- is.na(data$grades_score) == F & 
  !is.na(data$ssi_other_race_negro_na_2_std) == 1 & data$negro_na == 1 & 
  data$grades_score > -2 & data$grades_score < 2 & 
  data$flag_select_sample == 0 &
  is.na(data$id_interview) == F

plots_nw_other <- create_plots_data_np(data$grades_score, 
                                       data$ssi_other_race_negro_na_2_std, 
                                       criteria_selection_nw_other)

## All race friends: 
criteria_selection_wh_other <- is.na(data$grades_score) == F & 
  !is.na(data$ssi_all_race_2_std) == 1 & data$race_simple == 1 & 
  data$grades_score > -2 & data$grades_score < 2 & 
  data$flag_select_sample == 0 & 
  is.na(data$id_interview) == F

plots_wh_all <- create_plots_data_np(data$grades_score,
                                     data$ssi_all_race_2_std,
                                     criteria_selection_wh)

criteria_selection_nw_all <- is.na(data$grades_score) == F & 
  !is.na(data$ssi_all_race_2_std) == 1 & data$negro_na == 1 & 
  data$grades_score > -2 & data$grades_score < 2 & 
  data$flag_select_sample == 0 &
  is.na(data$id_interview) == F

plots_nw_all <- create_plots_data_np(data$grades_score, 
                                     data$ssi_all_race_2_std, 
                                     criteria_selection_nw_all)


### Plotting with ggplot -------
# Create data frame same race
nonwhite_vector <- rep("Nonwhite", nrow(plots_nw[[1]]))
white_vector <- rep("White", nrow(plots_wh[[1]]))

nonwhite_new <- cbind(plots_nw[[1]], nonwhite_vector)
white_new <- cbind(plots_wh[[1]], white_vector)

colnames(nonwhite_new) <- c("grades", "fit_1", "fit_2", "fit_3", "race")
colnames(white_new) <- c("grades", "fit_1", "fit_2", "fit_3", "race")

final_new <- rbind(nonwhite_new, white_new) 
same_race <- rep("Same race", nrow(final_new))

final_new <- cbind(final_new, same_race)

# Create data frame other race
nonwhite_other <- cbind(plots_nw_other[[1]], nonwhite_vector)
white_other <- cbind(plots_wh_other[[1]], white_vector)

colnames(nonwhite_other) <- c("grades", "fit_1", "fit_2", "fit_3", "race")
colnames(white_other) <- c("grades", "fit_1", "fit_2", "fit_3", "race")

final_other <- rbind(nonwhite_other, white_other) 
other_race <- rep("Other race", nrow(final_other))
final_other <- cbind(final_other, other_race)

# Create data frame all race
nonwhite_all <- cbind(plots_nw_all[[1]], nonwhite_vector)
white_all <- cbind(plots_wh_all[[1]], white_vector)

colnames(nonwhite_all) <- c("grades", "fit_1", "fit_2", "fit_3", "race")
colnames(white_all) <- c("grades", "fit_1", "fit_2", "fit_3", "race")

final_all <- rbind(nonwhite_all, white_all) 
all_race <- rep("All race", nrow(final_all))
final_all <- cbind(final_all, all_race)
colnames(final_new)[6] <- "social_status"
colnames(final_other)[6] <- "social_status"
colnames(final_all)[6] <- "social_status"

final_df <- rbind(final_new, final_other, final_all)

ggplot(data = final_df, aes(grades, fit_2, color = race)) + 
  geom_line(size = 1) + scale_colour_colorblind() +
  theme_classic(base_size = 12) +
  labs(title="Same race social status",
       x="Grades", y = "Social Status", 
       color = "Race") +
  facet_grid(. ~ social_status, scales = "free_x")

font_size <- 6

t1_nw <- final_df %>% subset(social_status == "Same race") %>%
  ggplot(aes(grades, fit_2, color = race)) + 
  geom_line(size = 1) + scale_colour_colorblind() + 
  theme_classic(base_size = font_size) +
  theme(legend.position = "none", 
        legend.text = element_text(size = font_size), 
        legend.title = element_text(size = font_size)) +
  labs(title="Same race", x="Grades", y = "Social Status", color = "Race")

t2_nw <- final_df %>% subset(social_status == "Other race") %>% 
  ggplot(aes(grades, fit_2, color = race)) + 
  geom_line(size = 1) + scale_colour_colorblind() + 
  theme_classic(base_size = font_size) +
  theme(legend.position = "none", 
        legend.text = element_text(size = font_size), 
        legend.title = element_text(size = font_size)) +
  labs(title="Other race", x="Grades", y = "Social Status", color = "Race")

t3_nw <- final_df %>% subset(social_status == "All race") %>% 
  ggplot(aes(grades, fit_2, color = race)) + 
  geom_line(size = 1) + scale_colour_colorblind() + 
  theme_classic(base_size = font_size) +
  theme(legend.position = "none", 
        legend.text = element_text(size = font_size), 
        legend.title = element_text(size = font_size)) +
  labs(title="All races", x="Grades", y = "Social Status", color = "Race")

gg_np_same_other_all_race_nw_2 <- ggarrange(t3_nw, t1_nw, t2_nw, 
                                            nrow = 1, legend = "bottom", 
                                            common.legend = T) 

# pdf("figures/gg_np_same_other_all_race_nw_2.pdf",  width = 24, height = 8)
# print(gg_np_same_other_all_race_nw_2)
# dev.off()

# pdf("figures/gg_np_same_other_all_race_nw_2_pnas.pdf",  
#     width = 8.7/2.54, height = 4/2.54)
# print(gg_np_same_other_all_race_nw_2)
# dev.off()

# pdf("figures/gg_np_same_other_all_race_nw_2_pnas_medium.pdf",  
#     width = 11.4/2.54, height = 6/2.54)
# print(gg_np_same_other_all_race_nw_2)
# dev.off()

pdf("figures/gg_np_same_other_all_race_nw_2_pnas_large.pdf",
    width = 17.8/2.54, height = 6/2.54)
print(gg_np_same_other_all_race_nw_2)
dev.off()


# ggsave(filename = "figures/gg_np_same_other_all_race_nw_2_pnas_large_dpi.pdf",
#        plot = gg_np_same_other_all_race_nw_2,
#        width = 17.8, height = 6, units = "cm", dpi = 400)
# 
# ggsave(filename = "figures/gg_np_same_other_all_race_nw_2_pnas_large_dpi_2.eps",
#        plot = gg_np_same_other_all_race_nw_2,
#          width = 17.8, height = 6, units = "cm", dpi = 1200)
# 
